package com.jdk.demo.reflect.clazz;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import com.jdk.demo.reflect.User;

public class ClassForname {

	public static void main(String[] args) throws Exception {
		Class<?> c = Class.forName("com.jdk.demo.reflect.User");
		User u = (User) c.newInstance();
		System.out.println(u);

		Method m = c.getDeclaredMethod("setName", new Class[]{String.class});
		m.invoke(u, new Object[]{"zzz"});
		System.out.println(u.getName());

		Method m2 = c.getMethod("setId", new Class[]{Integer.class});
		System.out.println(m2.getName());

		Field f1 = c.getField("age");
		System.out.println(f1);
		Field f2 = c.getField("id");
		System.out.println(f2);
		// 获得指定的私有属性
		Field f3 = c.getDeclaredField("name");
		// 启用和禁用访问安全检查的开关，值为 true，则表示反射的对象在使用时应该取消java语言的访问检查；反之不取消
		f3.setAccessible(true);
		f3.set(u, "sss");
		System.out.println(u.getName());
		System.out.println(f3);
	}

}
